<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_INS_SHOP.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_INS_SHOP,$login['staff_auth']);

$dosearch = FALSE ;

$dao = new Class_M_INS_SHOP;

$pattern  = Class_M_OTHER::get_pattern_array(NULL, '23' );

//$selectarr = array(""=>PLEASE_SELECT_ARR, 0=>NONE_SELECT_ARR);
//$patterns = array_merge($selectarr,$pattern);
$patterns[""] =  PLEASE_SELECT_ARR;
$patterns[0]  =  NONE_SELECT_ARR;
foreach ($pattern as $k =>$v){
	$patterns[$k] = $v;
}
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['INS_SHOP'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['INS_SHOP'] = $forms;
}
$isG4  = FALSE;
if   ($login['staff_auth'] == 'G-4') {
	$isG4 = TRUE;
}

$isG5  = FALSE;
if   ($login['staff_auth'] == 'G-5') {
	$isG5 = TRUE;
}
$wherearr =  array();
if ( $isG4 ) {
		$wherearr[] = sprintf(" ins_co_cd =  '%s' ",  $_SESSION['ADMIN_LOGIN']['STAFF_INS_CO_CD'] );
} else {
	if ( isset( $forms['ins_co_cd'] ) &&  ( !empty( $forms['ins_co_cd'] ) ) ){
		$wherearr[] = sprintf(" ins_co_cd =  '%s' ",  $dao->esc( $forms['ins_co_cd'] ) );
	}
}
if ( $isG5) {
	$wherearr[] = " area_pattern_cd > 0 ";
}
if ( isset( $forms['block_cd'] ) &&  ( !empty( $forms['block_cd'] ) ) ){
	$wherearr[] = sprintf(" block_cd =  '%s' ",  $dao->esc( $forms['block_cd'] ));
}

if ( isset( $forms['local_cd'] ) &&  ( !empty( $forms['local_cd'] ) ) ){
	$wherearr[] = sprintf(" local_cd =  '%s' ",  $dao->esc( $forms['local_cd'] ));
}

if ( isset( $forms['area_pattern_cd'] )  ){
	if (strlen($forms['area_pattern_cd']) >0) {
		$wherearr[] = sprintf(" area_pattern_cd =  '%s' ",  $forms['area_pattern_cd'] );
	}
}

if ( isset( $forms['media_name'] ) &&  ( !empty( $forms['media_name'] ) ) ){
	$wherearr[] = sprintf(" media_name like  '%s' ",  "%".$dao->esc( $forms['media_name'] )."%" );
}
if ( isset( $forms['ins_shop_cd'] ) &&  ( !empty( $forms['ins_shop_cd'] ) ) ){
	$wherearr[] = sprintf(" a.ins_shop_cd like  '%s' ",  "%".$dao->esc( $forms['ins_shop_cd'] )."%" );
}

if ( isset( $forms['ins_shop_name'] ) &&  ( !empty( $forms['ins_shop_name'] ) ) ){
	$wherearr[] = sprintf(" ins_shop_name like  '%s' ",  "%".$dao->esc( $forms['ins_shop_name'] )."%" );
}
if ( isset( $forms['stopflg'] ) &&  ( !empty( $forms['stopflg'] ) ) ){
	if ( $forms['stopflg'] == "1" ) {
		$wherearr[] = "( (stop_date > CURDATE() + 0) OR (stop_date = 0) )";
	} else {
		$wherearr[] = "(stop_date <= CURDATE() + 0 AND stop_date > 0)";
	}
}
if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1, 'a' );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'], 2, 'a' );
}


$block = Class_M_OTHER::get_block_array(PLEASE_SELECT_ARR);
$local = Class_M_OTHER::get_local_array( PLEASE_SELECT_ARR );

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download"   ) ){

	$_GET["csv"] = "";
	$tmpfilename = "ins_shop".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->search( $wherearr );
	
	//列名
	$colums = array();

 //1 string code 2 integer 3 date 4 string ""必要 
 $col_heads = array();
 if ($isG4) {
	$col_heads = array(
		'ins_shop_cd'    => array( 'GEOコード',       1),
		'ins_shop_name'  => array( '販売店名',        1),
		'ins_co_cd'      => array( '折込会社コード',  1),
		'ins_co_name'    => array( '折込会社名',      1),
		'block_cd'       => array( 'ブロックコード',  1),
		'block_name'     => array( 'ブロック名',      1),
		'local_cd'       => array( '都道府県コード',  1),
		'local_name'     => array( '都道府県名',      1),
		'media_name'     => array( '媒体名',          1),
		'delive_cd'      => array( '納品先コード',    1),
		'delive_name'    => array( '納品先',          1),
		'insert_conums'  => array( '折込会社部数',    2),
		'memo'           => array( '廃止コメント',    4),
		'u_time'         => array( '更新日時',        3),
		'c_time'         => array( '登録日時',        3),
		'm_user_name'    => array( '更新者',          1)
	);
 }elseif($isG5){
	$col_heads = array(
		'ins_shop_cd'    => array( 'GEOコード',       1),
		'ins_shop_name'  => array( '販売店名',        1),
		'ins_co_cd'      => array( '折込会社コード',  1),
		'ins_co_name'    => array( '折込会社名',      1),
		'block_cd'       => array( 'ブロックコード',  1),
		'block_name'     => array( 'ブロック名',      1),
		'local_cd'       => array( '都道府県コード',  1),
		'local_name'     => array( '都道府県名',      1),
		'media_name'     => array( '媒体名',          1),
		'area_pattern_cd'=> array( '地域広告有無(コード)',      1),
		'area_pattern_nm'=> array( '地域広告有無(地域新聞名)',  1),
		'area_adnums'    => array( '地域広告部数',      1),
		'area_memo'      => array( '地域広告備考',      1),
		'u_time'         => array( '更新日時',        3),
		'c_time'         => array( '登録日時',        3),
		'm_user_name'    => array( '更新者',          1)
	);
 }else {
	$col_heads = array(
		'ins_shop_cd'    => array( 'GEOコード',       1),
		'ins_shop_name'  => array( '販売店名',        1),
		'ins_co_cd'      => array( '折込会社コード',  1),
		'ins_co_name'    => array( '折込会社名',      1),
		'block_cd'       => array( 'ブロックコード',  1),
		'block_name'     => array( 'ブロック名',      1),
		'local_cd'       => array( '都道府県コード',  1),
		'local_name'     => array( '都道府県名',      1),
		'media_name'     => array( '媒体名',          1),
		'delive_cd'      => array( '納品先コード',    1),
		'delive_name'    => array( '納品先',          1),
		'insert_conums'  => array( '折込会社部数',    2),
		'area_pattern_cd'=> array( '地域広告有無(コード)',      1),
		'area_pattern_nm'=> array( '地域広告有無(地域新聞名)',  1),
		'area_adnums'    => array( '地域広告部数',      1),
		'area_memo'      => array( '地域広告備考',      1),
		'shop'           => array( '店舗名',           4),
		'aoki_compete'   => array( 'AOKI/競合(コード)', 1),
		'aoki_compete_nm'=> array( 'AOKI/競合',         1),
		'shop_away'      => array( '店舗からの距離',    2),
		'home_percent'   => array( '世帯普及率',        2),
		'all_amount'     => array( '総需要額',          2),
		'sale_share'     => array( '売上高シェア',      2),
		'stop_date'      => array( '廃止日',          3),
		'memo'           => array( '廃止コメント',    4),
		'u_time'         => array( '更新日時',        3),
		'c_time'         => array( '登録日時',        3),
		'm_user_name'    => array( '更新者',          1)
	);
 }
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}

	//データ整形開始
	foreach ($data as $k => $v) {
    	$data[$k]["stop"] =  str_replace(',', '\r\n', $v["stop"]);
		$data[$k]["area_pattern_nm"] = $pattern[$v["area_pattern_cd"]];
		$data[$k]["aoki_compete_nm"] = $aoki_compete_sort[$v["aoki_compete"]];
		$data[$k]["block_name"] = $v["block_cd"] == "" ? "": $block[$v["block_cd"]] ;
		$data[$k]["local_name"] = $v["local_cd"] == "" ? "": $local[$v["local_cd"]] ;
		
		
		$data[$k]["stop_date"] =  Class_PWD::int8_to_date($v["stop_date"]);
		$data[$k]["c_time"]    =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]    =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了

	$dao->output_csv($data, $colums);
	exit;
}
/*
//地域エリアＣＳＶ
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "downloada"   ) ){

	$_GET["csv"] = "";
	$tmpfilename = "ins_shop_area".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->search( $wherearr );
	
	//列名
	$colums = array();
 //1 string code 2 integer 3 date 4 string ""必要 
	$col_heads = array(
		'ins_shop_cd'      => array( '販売店コード',         1),
		'area_pattern_cd'  => array( '地域広告パタンコード', 2),
		'area_adnums'      => array( '地域広告部数',         2),
		'area_memo'        => array( '地域広告備考',         1)
	);
	
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	
	//データ整形完了

	$dao->output_csv($data, $colums);
	exit;
}
*/
//---CSVダウンロード　処理完了
if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];
	$st = ( $page['current'] -1 ) * PAGE_MAX;
}else{
	$page['current'] = 1;
	$st = 0;
}

$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );
	$dateyymmdd= date(Ymd,time());
	
	foreach ($data as $k=>$v) {
		

		$data[$k]["block_name"] = $v["block_cd"] == "" ? "": $block[$v["block_cd"]] ;
		$data[$k]["local_name"] = $v["local_cd"] == "" ? "": $local[$v["local_cd"]] ;
		$data[$k]["pattern_name"] = $v["area_pattern_cd"] == "" ? "": $patterns[$v["area_pattern_cd"]] ;
		if ($v["stop_date"] != 0){
			if ($v["stop_date"]<$dateyymmdd){
				$data[$k]["stop"] = "廃止";
			}	
		}	
	}
}


//$select = array(''=>"選択してください");
//折込会社の場合、折込会社の自社です。

$ins_co = Class_M_OTHER::get_ins_co_array(PLEASE_SELECT_ARR);

if ($isG4){
	 $forms["ins_co_cd"] = $_SESSION['ADMIN_LOGIN']['STAFF_INS_CO_CD'] ;
	 $forms["ins_co_name"]  = $ins_co[$forms["ins_co_cd"]];
}

$smarty->assign('isG4', $isG4 );
$smarty->assign('isG5', $isG5 );
if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) ) $smarty->assign('data', $data);
if( isset($page) ) $smarty->assign('page', $page);


$smarty->assign('ins_co', $ins_co );

$smarty->assign('local', $local );

$stopflags[""] = "すべて";

foreach ($aoki_stopdate_flags as $k => $v) {
	$stopflags[$k] = $v;
}

$smarty->assign('stopflags', $stopflags );

$smarty->assign('block', $block );

$smarty->assign('pattern', $patterns );
	

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );

$smarty->display('minsshop_index.html');

?>